﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using XKPlatform.DataModels.ReturnModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//using MQHelper.MQ;
using System.Text;
using XKPlatform.Common;
using XKPlatform.Common.Enums;

namespace XKPlatform.Filter
{
    public class APIActionFilter : ActionFilterAttribute
    {

        public override void OnActionExecuted(ActionExecutedContext context)
        {
            //throw new NotImplementedException();
            
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            
            var modelState = context.ModelState;
            if (!modelState.IsValid)
            {
                string error = string.Empty;
                foreach (var key in modelState.Keys)
                {
                    var state = modelState[key];
                    if (state.Errors.Any())
                    {
                        error = state.Errors.First().ErrorMessage;
                        break;
                    }
                }
                BaseModel result = new BaseModel() { code = -1, msg = error };
                Console.WriteLine(error);
                context.Result = new JsonResult(result);
            }
        }

       
    } 
}
